package com.woniu.stafforder.controller;

import com.woniu.stafforder.dto.TelModelDto;
import com.woniu.stafforder.util.TelModelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.InputStreamSource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import javax.mail.internet.MimeMessage;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;


@Api(value = "BillController", tags = "生成发票接口")
@RestController
public class BillController {
    @Autowired
    private JavaMailSender javaMailSender;
    @ApiOperation(value = "生成用户发票")
    @PostMapping("/generationBill")
    public ResponseEntity<byte[]> generationBill(@RequestBody TelModelDto telModelDto) {
        OutputStream outputStream = TelModelUtil.generateTelModel(telModelDto);
        byte[] byteArray = ((ByteArrayOutputStream) outputStream).toByteArray();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_PDF);
        //发票文件名为订单编号
        headers.setContentDispositionFormData("attachment", telModelDto.getOrderId() + ".pdf");
        headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
        //发送邮箱给用户
        sendEmail(telModelDto.getOrderId(), new ByteArrayResource(byteArray), telModelDto.getUserEmail());
        return new ResponseEntity<>(byteArray, headers, HttpStatus.OK);
    }



    /**
     * 发送邮件
     * @param orderId
     * @param bill
     * @param userEmail
     * @return
     */
    @SneakyThrows
    public String sendEmail(String orderId, InputStreamSource bill, String userEmail) {
        //邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        //标题
        helper.setSubject("中国移动有限公司电子发票");
        //内容
        helper.setText("<!DOCTYPE html>\n" +
                "<html lang=\"zh-CN\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <title>中国移动发票</title>\n" +
                "    <style>\n" +
                "        body {\n" +
                "            font-family: Arial, sans-serif;\n" +
                "            margin: 0;\n" +
                "            padding: 20px;\n" +
                "            background-color: #f4f4f4;\n" +
                "            color: #333;\n" +
                "        }\n" +
                "        .container {\n" +
                "            max-width: 600px;\n" +
                "            margin: 0 auto;\n" +
                "            background-color: #fff;\n" +
                "            padding: 20px;\n" +
                "            border-radius: 8px;\n" +
                "            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);\n" +
                "        }\n" +
                "        .header {\n" +
                "            text-align: center;\n" +
                "            padding-bottom: 20px;\n" +
                "            border-bottom: 1px solid #eaeaea;\n" +
                "        }\n" +
                "        .header h1 {\n" +
                "            margin: 0;\n" +
                "            color: #0066cc;\n" +
                "        }\n" +
                "        .content {\n" +
                "            padding: 20px 0;\n" +
                "        }\n" +
                "        .content h2 {\n" +
                "            margin: 0 0 10px;\n" +
                "            color: #0066cc;\n" +
                "        }\n" +
                "        .content p {\n" +
                "            line-height: 1.6;\n" +
                "            margin: 0 0 10px;\n" +
                "        }\n" +
                "        .footer {\n" +
                "            text-align: center;\n" +
                "            padding-top: 20px;\n" +
                "            border-top: 1px solid #eaeaea;\n" +
                "            color: #999;\n" +
                "        }\n" +
                "    </style>\n" +
                "</head>\n" +
                "<body>\n" +
                "    <div class=\"container\">\n" +
                "        <div class=\"header\">\n" +
                "            <h1>中国移动发票</h1>\n" +
                "        </div>\n" +
                "        <div class=\"content\">\n" +
                "            <h2>尊敬的客户，您好：</h2>\n" +
                "            <p>感谢您使用中国移动的服务。请查收附件中的发票。</p>\n" +
                "            <p>如果您有任何疑问，请拨打我们的客户服务热线：10086。</p>\n" +
                "        </div>\n" +
                "        <div class=\"footer\">\n" +
                "            <p>&copy; 2024 中国移动通信集团有限公司. 保留所有权利。</p>\n" +
                "        </div>\n" +
                "    </div>\n" +
                "</body>\n" +
                "</html>", true);
        helper.setFrom("2557726587@qq.com");
        helper.setTo(userEmail);
        //文件名为订单编号
        helper.addAttachment(orderId + ".pdf", bill);
        javaMailSender.send(mimeMessage);
        return "发送成功";
    }
}